$(function () {

    initArtCateList()
    const layer = layui.layer
    function initArtCateList() {
        axios({
            url: '/my/article/cates',
        }).then(res => {
            //成功回调

            let dataArr = res.data.data
            if (res.data.status != 0) return layer.msg(res.data.message)
            //渲染
            let htmlArr = []
            dataArr.forEach(value => {
                htmlArr.push(`
                    <tr>
                        <td>${value.Id}</td>
                        <td>${value.name}</td>
                        <td>${value.alias}</td>
                        <td>
                            <button type="button" data-id=${value.Id} class="btn-edit layui-btn layui-btn-xs">修改</button>
                            <button type="button" data-id=${value.Id} class="del-edit layui-btn layui-btn-xs layui-btn-danger">删除</button>
                        </td>
                    </tr>
                `)
            })
            $('tbody').empty().html(htmlArr)

        })
    }
    let indexAdd = 0
    $('#addBtn').on('click', function () {
        //弹出框
        indexAdd = layer.open({
            type: 1,
            title: '添加文章分类',
            area: ['500px', '260px'],
            content: `
                <form id="addForm" class="layui-form" action="">
                        <!-- 1.分类 -->
                        <div class="layui-form-item">
                            <label class="layui-form-label">分类名称</label>
                            <div class="layui-input-block">
                                <input type="text" name="name" required lay-verify="required" placeholder="请输入分类名称" autocomplete="off"
                                    class="layui-input">
                            </div>
                        </div>
                        <!-- 2.别名 -->
                        <div class="layui-form-item">
                            <label class="layui-form-label">分类别名</label>
                            <div class="layui-input-block">
                                <input type="text" name="alias" required lay-verify="required" placeholder="请输入分类别名" autocomplete="off"
                                    class="layui-input">
                            </div>
                        </div>
                        <!-- 3.按钮 -->
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                            </div>
                        </div>
                </form>
        `,
        })
    })
    //事件委托提交
    //新增
    $('body').on('submit', '#addForm', function (e) {
        e.preventDefault()
        axios({
            url: '/my/article/addcates',
            method: 'post',
            data: $(this).serialize()

        }).then(res => {
            //成功回调
            if (res.data.status != 0) {
                return layer.msg(res.data.message)
            }
            layer.msg('恭喜你 ! &nbsp;' + res.data.message)
            //刷新页面
            initArtCateList()
            //关闭弹出层
            layer.close(indexAdd)
        })
    })

    let indexEdit = 0
    //点击修改按钮弹出修改框
    $('tbody').on('click', '.btn-edit', function () {
        //弹出框
        indexEdit = layer.open({
            type: 1,
            title: '修改文章分类',
            area: ['500px', '260px'],
            content: `
                <form id="editForm" class="layui-form" action="" lay-filter="editForm">
                        <!-- 0.Id -->
                        <input type="hidden" name="Id">
                        <!-- 1.分类 -->
                        <div class="layui-form-item">
                            <label class="layui-form-label">分类名称</label>
                            <div class="layui-input-block">
                                <input type="text" name="name" required lay-verify="required" placeholder="请输入分类名称" autocomplete="off"
                                    class="layui-input">
                            </div>
                        </div>
                        <!-- 2.别名 -->
                        <div class="layui-form-item">
                            <label class="layui-form-label">分类别名</label>
                            <div class="layui-input-block">
                                <input type="text" name="alias" required lay-verify="required" placeholder="请输入分类别名" autocomplete="off"
                                    class="layui-input">
                            </div>
                        </div>
                        <!-- 3.按钮 -->
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="editForm layui-btn" lay-submit lay-filter="formDemo">立即修改</button>
                            </div>
                        </div>
                </form>
        `
        })
        //自定义属性发送Ajax
        let id = $(this).attr('data-id')
        axios({
            url: '/my/article/cates/' + id,
            method: 'get',
        }).then(res => {
            //成功回调
            if (res.data.status != 0) {
                return layer.msg(res.data.message)
            }
            //渲染数据
            layui.form.val('editForm', res.data.data)
        })

    })

    //修改
    $('body').on('submit', '#editForm', function (e) {
        e.preventDefault()
        axios({
            url: '/my/article/updatecate',
            method: 'post',
            data: $(this).serialize()

        }).then(res => {
            //成功回调
            console.log(res)
            if (res.data.status != 0) {
                return layer.msg(res.data.message)
            }
            layer.msg('恭喜你 ! &nbsp;' + res.data.message)
            //刷新页面
            initArtCateList()
            //关闭弹出层
            layer.close(indexEdit)
        })
    })

    //删除事件
    $('body').on('click', '.del-edit', function () {
        let id = $(this).attr('data-id')
        //eg1
        layer.confirm('是否确认删除?', {
            icon: 3,
            title: '提示'
        }, function (index) {
            //do something
            axios({
                url: '/my/article/deletecate/' + id,
                method: 'get',

            }).then(res => {
                if (res.data.status != 0) {
                    return layer.msg(res.data.message)
                }
                //成功提示
                layer.msg(res.data.message)
            })
            layer.close(index);
            //渲染数据
            initArtCateList()
        })
        

    })

})